package com.shijie.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Transient;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Condition;

import com.shijie.component.ErpUserService;
import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;
import com.shijie.domain.entity.ActivityProducts;
import com.shijie.domain.entity.Activitys;
import com.shijie.mapper.ActivityProductsMapper;
import com.shijie.mapper.ActivitysMapper;

/**
 * Created by DELL on 2018/8/13.
 */
@Service
public class ActivitysService extends AbstractService<Activitys> {

    @Resource
    private ActivitysMapper activitysMapper;
    @Resource
    private ActivityProductsMapper  activityProductsMapper;
    @Resource
    private ErpUserService erpUserService;
    

    public List<Activitys> listByProductId(Integer productId) {
        return activitysMapper.listByProductId(productId);
    }
    
    public List<ActivityProducts> listProductByActiveId(Integer activeId) {
    	Condition condition = new Condition(ActivityProducts.class);
		condition.createCriteria().andEqualTo("activityId", activeId);
		return activityProductsMapper.selectByCondition(condition);
    }
    
    
    @Transient
    public void save(Activitys activitys){
    	if(activitys.getProductIdList() == null || activitys.getProductIdList().size() <1){
    		throw new ServiceException("新增活动时必须选择产品！");
    	}
    	if(activitys.getId() == null){
    		activitys.setCreateBy(erpUserService.getUserName());
    		activitys.setCreateTime(DateTime.now().toDate());
    		activitysMapper.insert(activitys);
    		List<ActivityProducts> activityProducts = new ArrayList<>();
    		for(Integer id : activitys.getProductIdList()){
    			ActivityProducts a = new ActivityProducts();
    			a.setActivityId(activitys.getId());
    			a.setProductId(id);
    			a.setCreateBy(erpUserService.getUserName());
        		a.setCreateTime(DateTime.now().toDate());
    			activityProducts.add(a);
    		}
    		activityProductsMapper.insertList(activityProducts);
    	}else{
    		Condition condition = new Condition(ActivityProducts.class);
    		condition.createCriteria().andEqualTo("activityId", activitys.getId());
    		activityProductsMapper.deleteByCondition(condition);
    		
    		activitys.setUpdateBy(erpUserService.getUserName());
    		activitys.setUpdateTime(DateTime.now().toDate());
    		activitysMapper.updateByPrimaryKey(activitys);
    		
    		List<ActivityProducts> activityProducts = new ArrayList<>();
    		for(Integer id : activitys.getProductIdList()){
    			ActivityProducts a = new ActivityProducts();
    			a.setActivityId(activitys.getId());
    			a.setProductId(id);
    			activityProducts.add(a);
    		}
    		activityProductsMapper.insertList(activityProducts);
    	}
    }
    
    @Transactional
    public void deleteById(Integer id){
    	activitysMapper.deleteByPrimaryKey(id);
    	Condition condition =new Condition(ActivityProducts.class);
    	ActivityProducts ap = new ActivityProducts();
    	ap.setActivityId(id);
    	activityProductsMapper.deleteByCondition(condition);
    }

}
